# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from datetime import datetime
from dateutil.relativedelta import relativedelta

from openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _
from re import search

class account_asset_category(osv.osv):
    _inherit = 'account.asset.category'

    _defaults = {
        'prorata': True,
    }
    
class account_asset_asset(osv.osv):
    _inherit = 'account.asset.asset'
    
    def unposted_all_moves(self, cr, uid, ids, context=None):

        depreciation_line_obj = self.pool.get('account.asset.depreciation.line')
        for asset in self.browse(cr, uid, ids, context=context):
            for line in asset.depreciation_line_ids:
               depreciation_line_obj.unposted_move(cr, uid, [line.id,], context=context)
               depreciation_line_obj.unlink(cr, uid, line.id, context=context)
        return True
    
    def check_posted_moves(self, cr, uid, ids, context=None):

        depreciation_line_obj = self.pool.get('account.asset.depreciation.line')
        for asset in self.browse(cr, uid, ids, context=context):
            for line in asset.depreciation_line_ids:
               if line.move_check != True:
                   raise osv.except_osv(_('Invalid action !'), _('You have not posted all depreciation entries, Please check carefully before closing'))
        return True
    
    def set_to_close(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        
        self.check_posted_moves(cr, uid, ids, context=context)
        
        return self.write(cr, uid, ids, {'state': 'close'}, context=context)
    
    def set_to_draft(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        
        self.unposted_all_moves(cr, uid, ids, context=context)
           
        return self.write(cr, uid, ids, {'state': 'draft'}, context=context)
      
    def validate(self, cr, uid, ids, context=None):
        self.compute_depreciation_board(cr, uid, ids, context=context)
        if context is None:
            context = {}
        return self.write(cr, uid, ids, {
            'state':'open'
        }, context)
    
    def create(self, cr, uid, vals, context=None):
        depreciation_lin_obj = self.pool.get('account.asset.depreciation.line')
        asset_id = super(account_asset_asset, self).create(cr, uid, vals, context=context)
        old_depreciation_line_ids = depreciation_lin_obj.search(cr, uid, [('asset_id', '=', asset_id), ('move_id', '=', False)])
        if old_depreciation_line_ids:
            depreciation_lin_obj.unlink(cr, uid, old_depreciation_line_ids, context=context)

        return asset_id
     
    def _compute_board_amount(self, cr, uid, asset, i, residual_amount, amount_to_depr, undone_dotation_number, posted_depreciation_line_ids, total_days, depreciation_date, context=None):
        amount = 0
        if i == undone_dotation_number:
            amount = residual_amount
        else:
            if asset.method == 'linear':
                amount = amount_to_depr / (undone_dotation_number - len(posted_depreciation_line_ids))
                if asset.prorata:
                    amount = amount_to_depr / asset.method_number
        return amount
        
    def _compute_board_undone_dotation_nb(self, cr, uid, asset, depreciation_date, total_days, context=None):
        undone_dotation_number = asset.method_number
            
        return undone_dotation_number
    
    def _compute_entries(self, cr, uid, ids, period_id, context=None):
        result = []
        period_obj = self.pool.get('account.period')
        depreciation_obj = self.pool.get('account.asset.depreciation.line')
        period = period_obj.browse(cr, uid, period_id, context=context)
        depreciation_ids = depreciation_obj.search(cr, uid, [('asset_id', 'in', ids), ('depreciation_date', '<=', period.date_stop), ('move_check', '=', False)], context=context)
        context = dict(context or {}, depreciation_date=period.date_stop)
        return depreciation_obj.create_move(cr, uid, depreciation_ids, context=context)
    
class account_asset_depreciation_line(osv.osv):
    _inherit = 'account.asset.depreciation.line'
    
    def unposted_move(self, cr, uid, ids, context=None):
        context = dict(context or {})
        can_close = False
        move_obj = self.pool.get('account.move')

        for line in self.browse(cr, uid, ids, context=context):
            if line.move_id:
                move_obj.unlink(cr, uid, line.move_id.id, context=context)
                self.write(cr, uid, line.id, {'move_check':False}, context=context)

        return True
    
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
